package view.book;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import entity.Book;
import entity.BookType;
import service.book.BookServer;
import service.book.BookServerImpl;
import utils.ComonUtil;
import view.MainView;
import view.CommonView.CommonJPanel;
import view.model.BookTableModel;
import view.model.BookTypeComboBoxModel;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 修改图书模块
 *
 */
public class EditBookView extends CommonJPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BookServer bookServer = new BookServerImpl();

	// 创建表格
	private JTable table;
	private BookTableModel bookTableModel;

	// 创建输入框
	private JTextField bookNameField, authorField, editBookIdField, editAuthorField, editPriceField, editBookNameField,
			editPressField, editCountField, editPressTimeField;
    // JComboBox<BookType> editBookTypeField;
	private JTextArea editBookDescField;

	// 创建按钮
	private JButton edit, delete, searchButton;

	private BookTypeComboBoxModel editBookTypeComboBoxModel = new BookTypeComboBoxModel();
	private JComboBox<BookType> editBookTypeComboBox;

	public EditBookView() {
		super(TYPE_TRANSPARENT);
		setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));

		// 标签、输入框面板
		JPanel searchPanel = new CommonJPanel(TYPE_TRANSPARENT);
		searchPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 20));
		searchPanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 70));
		add(searchPanel);

		// 创建图书名称标签
		JLabel labelBookName = new JLabel("图书名称：");
		searchPanel.add(labelBookName);
		// 创建图书名称输入框
		bookNameField = new JTextField();
		bookNameField.setPreferredSize(new Dimension(120, 30));
		searchPanel.add(bookNameField);

		// 创建图书作者标签
		JLabel labelAuthor = new JLabel("图书作者：");
		searchPanel.add(labelAuthor);
		// 创建图书作者输入框
		authorField = new JTextField();
		authorField.setPreferredSize(new Dimension(120, 30));
		searchPanel.add(authorField);

		//		// 创建图书类别标签
		//		JLabel labelBookType = new JLabel("图书类别：");
		//		searchPanel.add(labelBookType);
		//		// 创建图书类别下拉框
		//		bookTypeComboBox = new JComboBox<>(editBookTypeComboBoxModel);
		//		bookTypeComboBox.setPreferredSize(new Dimension(120, 30));
		//		bookTypeComboBox.setBackground(Color.white);
		//		searchPanel.add(bookTypeComboBox);

		// 创建按钮
		searchButton = new JButton("查询");
		searchButton.setPreferredSize(new Dimension(100, 30));
		searchPanel.add(searchButton);

		// 创建表格面板
		JPanel tablePanel = new CommonJPanel(TYPE_TRANSPARENT);
		add(tablePanel);

		// 创建表格
		bookTableModel = new BookTableModel();
		table = new JTable(bookTableModel);
		JScrollPane sp = new JScrollPane(table);
		sp.setPreferredSize(new Dimension(750, 180));
		sp.setBackground(new Color(246, 246, 246));
		add(sp);

		// 数据显示区域面板
		JPanel editPanel = new CommonJPanel(TYPE_TRANSPARENT);
		editPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));
		editPanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 280));
		add(editPanel);

		// 创建编号标签、文本框
		JLabel labelBookId = new JLabel("图书编号：");
		editPanel.add(labelBookId);
		// 创建编号文本框
		editBookIdField = new JTextField();
		editBookIdField.setPreferredSize(new Dimension(260, 30));
		editBookIdField.setEditable(false);
		editPanel.add(editBookIdField);

		// 创建名称标签、文本框
		JLabel labelEditBookName = new JLabel("图书名称：");
		editPanel.add(labelEditBookName);
		// 创建名称文本框
		editBookNameField = new JTextField();
		editBookNameField.setPreferredSize(new Dimension(260, 30));
		editPanel.add(editBookNameField);

		// 创建作者标签、文本框
		JLabel labelEditAuthor = new JLabel("图书作者：");
		editPanel.add(labelEditAuthor);
		// 创建作者文本框
		editAuthorField = new JTextField();
		editAuthorField.setPreferredSize(new Dimension(260, 30));
		editPanel.add(editAuthorField);

		// 创建价格标签、文本框
		JLabel labelPrice = new JLabel("图书价格：");
		editPanel.add(labelPrice);
		// 创建价格文本框
		editPriceField = new JTextField();
		editPriceField.setPreferredSize(new Dimension(260, 30));
		editPanel.add(editPriceField);

		// 创建类别标签、文本框
		JLabel labelEditBookType = new JLabel("图书类别：");
		editPanel.add(labelEditBookType);
		// 创建类别文本框
		editBookTypeComboBox = new JComboBox<>(editBookTypeComboBoxModel);
		editBookTypeComboBox.setPreferredSize(new Dimension(260, 30));
		editBookTypeComboBox.setBackground(Color.white);
		editPanel.add(editBookTypeComboBox);
		//		JTextField qqeditPriceField = new JTextField();
		//		qqeditPriceField.setPreferredSize(new Dimension(260, 30));
		//		editPanel.add(qqeditPriceField);

		// 创建库存标签、文本框
		JLabel labelCount = new JLabel("图书库存：");
		editPanel.add(labelCount);
		// 创建库存文本框
		editCountField = new JTextField();
		editCountField.setPreferredSize(new Dimension(260, 30));
		editPanel.add(editCountField);

		// 出版
		JLabel labelPress = new JLabel("出版社    ：");
		editPanel.add(labelPress);
		editPressField = new JTextField();
		editPressField.setPreferredSize(new Dimension(260, 30));
		editPanel.add(editPressField);
		JLabel labelPressTime = new JLabel("出版时间：");
		editPanel.add(labelPressTime);
		editPressTimeField = new JTextField();
		editPressTimeField.setPreferredSize(new Dimension(260, 30));
		editPanel.add(editPressTimeField);

		// 创建图书描述标签
		JLabel labelBookDesc = new JLabel("图书描述：");
		editPanel.add(labelBookDesc);
		// 创建图书描述输入框
		editBookDescField = new JTextArea();
		editBookDescField.setPreferredSize(new Dimension(688, 80));
		editBookDescField.setLineWrap(true);
		editBookDescField.setBackground(new Color(246, 246, 246));
		editPanel.add(editBookDescField);

		// 创建按钮面板
		JPanel buttonPanel = new CommonJPanel(TYPE_TRANSPARENT);
		buttonPanel.setLayout(new GridLayout(1, 2, 80, 0));
		add(buttonPanel);

		// 创建登录按钮
		edit = new JButton("修改");
		buttonPanel.add(edit);

		// 创建重置按钮
		delete = new JButton("删除");
		buttonPanel.add(delete);

		// 添加表格监听器
		addTableListener();

		// 修改按钮事件
		addModifyListener();

		// 删除按钮事件
		addDeleteListener();
		
		//查询
		addSearchListener();

	}

	/**
	 * 更新数据
	 */
	public void updateData() {
		Book book = new Book();
		book.setBookName(bookNameField.getText());
		book.setBookAuthor(authorField.getText());
		BookTableModel.cs = bookServer.getBookList(book);
		table.updateUI();
		// resetInput();	
	}

	public void resetInput() {
		editBookIdField.setText("");
		editAuthorField.setText("");
		editPriceField.setText("");
		editBookNameField.setText("");
		editPressField.setText("");
		editCountField.setText("");
		editPressTimeField.setText("");
		editBookDescField.setText("");
		editBookTypeComboBox.setSelectedIndex(-1);
		editBookTypeComboBox.updateUI();
	}

	/**
	 * 获取表格选中当前行的数据
	 * 
	 * @return
	 */
    public Book getSelectBook() {
        int index = table.getSelectedRow();
        return BookTableModel.cs.get(index);
    }
    /**
     * @ 设置书本数据
     */
    public void setBookData(Book book) {
    	editBookIdField.setText(String.valueOf(book.getId()));
		editAuthorField.setText(book.getBookAuthor());
		editPriceField.setText(book.getBookPrice());
		editBookNameField.setText(book.getBookName());
		editPressField.setText(book.getBookPress());
		editCountField.setText(String.valueOf(book.getBorrowingNumbers()));
		editPressTimeField.setText(book.getBookPressTime());
		editBookDescField.setText(book.getBookDesc());
		
		int i = editBookTypeComboBoxModel.indexOf(book.getBookType());
		editBookTypeComboBox.setSelectedIndex(i);
		
		editBookTypeComboBox.updateUI();
    }
    

	/**
	 * 搜索事件
	 */
	public void addSearchListener() {
		searchButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				updateData();
				resetInput();

			}
		});
	}

	/**
	 * 修改按钮事件
	 */
	public void addModifyListener() {
		edit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (ComonUtil.isEmpty(editBookIdField.getText()) || ComonUtil.isEmpty(editAuthorField.getText())
						|| ComonUtil.isEmpty(editPriceField.getText())
						|| ComonUtil.isEmpty(editBookNameField.getText())
						|| ComonUtil.isEmpty(editCountField.getText())
						|| ComonUtil.isEmpty(editPressField.getText())
						|| ComonUtil.isEmpty(editPressTimeField.getText())
						|| ComonUtil.isEmpty(editBookDescField.getText())
						|| null == editBookTypeComboBox.getSelectedItem()
					) {
					JOptionPane.showMessageDialog(EditBookView.this, "数据不能为空，请先选择一条记录！");
					return;
				}

				Book book = new Book();
				book.setId(Integer.parseInt(editBookIdField.getText()));
				book.setBookAuthor(editAuthorField.getText());
				book.setBookPrice(editPriceField.getText());
				book.setBookName(editBookNameField.getText());
				book.setBorrowingNumbers(Integer.parseInt(editCountField.getText()));
				book.setBookPress(editPressField.getText());
				book.setBookPressTime(editPressTimeField.getText());
				book.setBookDesc(editBookDescField.getText());
				book.setBookType(editBookTypeComboBox.getSelectedItem().toString());
				String flag ="更新失败！";
				if(bookServer.upBook(book)) {
					flag = "更新成功！";
					updateData();
				}
				JOptionPane.showMessageDialog(EditBookView.this, flag);
			}
		});
	}

	/**
	 * 删除按钮事件
	 */
	public void addDeleteListener() {
		delete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (0 == editBookIdField.getText().length()) {
					JOptionPane.showMessageDialog(EditBookView.this, "请先选择一条记录！");
					return;
				}
		
				
				

				int choice = JOptionPane.showConfirmDialog(EditBookView.this, "是否删除？");
				if (choice != JOptionPane.OK_OPTION) {
					return;
				}
				
				
				String flag ="删除失败！";
				if(bookServer.deleteBook(Integer.parseInt(editBookIdField.getText()))) {
					flag = "删除成功！";
					updateData();
					resetInput();
				}
				JOptionPane.showMessageDialog(EditBookView.this, flag);
				
			}
		});
	}

	/**
	 * 表格点击事件
	 */
	public void addTableListener() {
		// 表格点击事件
		table.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				Book book = getSelectBook();
				
				setBookData(book);
			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}
		});

	}
	
	

}
